package com.chenille.tools.util

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.NonNull
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

/**
 * @Author: FALLS CHEN
 * @Date: 2020-01-08 18:39
 * EMAIL: 636e68747474403136332e636f6d
 * @Version 1.0
 */
@SuppressLint("StaticFieldLeak")
object Utils {


    @SuppressLint("StaticFieldLeak")
    private var context: Context? = null

    private fun Utils(): Nothing {
        throw UnsupportedOperationException("u can't instantiate me...")
    }


    fun inflate(resId: Int): View {
        return LayoutInflater.from(getContext()).inflate(resId, null)
    }

    /**
     * 初始化工具类
     *
     * @param context 上下文
     */
    fun init(@NonNull context: Context) {
        Utils.context = context.applicationContext
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    fun getContext(): Context {
        if (context != null) {
            return context as Context
        }
        throw NullPointerException("should be initialized in application")
    }


    fun check(o: Any?): Boolean = o == null

    /**
     * 将sp值转换为px值
     */
    fun sp2px(context: Context, spValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }

    /**
     * dip转px
     */
    fun dipToPx(context: Context, dip: Float): Int {
        return (dip * context.resources.displayMetrics.density + 0.5f).toInt()
    }


    fun getScreenPixWidth(context: Context): Int {
        return context.resources.displayMetrics.widthPixels
    }

    /**
     * dp转dip
     *
     * @param context
     * @param dp
     * @return
     */
    fun dp2px(context: Context, dp: Float): Int {
        val density = context.resources.displayMetrics.density
        return (dp * density + 0.5f).toInt()
    }


    /**
     * 颜色渐变，需要把ARGB分别拆开进行渐变
     */
    fun evaluateColor(startValue: Int, endValue: Int, fraction: Float): Int {
        if (fraction <= 0) {
            return startValue
        }
        if (fraction >= 1) {
            return endValue
        }
        val startA = startValue shr 24 and 0xff
        val startR = startValue shr 16 and 0xff
        val startG = startValue shr 8 and 0xff
        val startB = startValue and 0xff

        val endA = endValue shr 24 and 0xff
        val endR = endValue shr 16 and 0xff
        val endG = endValue shr 8 and 0xff
        val endB = endValue and 0xff

        return (startA + (fraction * (endA - startA)).toInt() shl 24
                or (startR + (fraction * (endR - startR)).toInt() shl 16)
                or (startG + (fraction * (endG - startG)).toInt() shl 8)
                or startB + (fraction * (endB - startB)).toInt())
    }


    /**
     * 产生一个倒计时的 Observable
     *
     * @param time
     * @return
     */
    fun countdown(time: Long): Observable<Long> {
        return Observable.interval(1, TimeUnit.SECONDS).map { aLong -> time - aLong }.take(time + 1)
    }
    fun countdownMilli(time: Long): Observable<Long> {
        return Observable.interval(1, TimeUnit.SECONDS).map { aLong -> time - aLong*1000 }.take(time + 1)
    }

    // 两次点击间隔不能少于1000ms
    @SuppressLint("StaticFieldLeak")
    private const val FAST_CLICK_DELAY_TIME = 1000
    private var lastClickTime: Long = 0
    fun isFastClick(): Boolean {
        var flag = true
        val currentClickTime = System.currentTimeMillis()
        if (currentClickTime - lastClickTime >= FAST_CLICK_DELAY_TIME) {
            flag = false
        }
        lastClickTime = currentClickTime
        return flag
    }




}